using System;
using System.Collections.Generic;
using System.Text;

namespace PBR
{
    public class BrushMipMap
    {
        private BitmapUnsafe imageS;
        private BitmapUnsafe imageM;
        private BitmapUnsafe imageL;
        private BitmapUnsafe imageXL;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="path"></param>
        public BrushMipMap(string path)
        {
            // imageXL
            imageXL = new BitmapUnsafe(path);

            float aspectRatio = (float)imageXL.Height / imageXL.Width;
            
            // imageL
            AForge.Imaging.Filters.ResizeBilinear resizeBilinearFilter = new AForge.Imaging.Filters.ResizeBilinear(imageXL.Width / 2, (int)(imageXL.Width / 2 * aspectRatio));
            imageL = new BitmapUnsafe(resizeBilinearFilter.Apply(imageXL.Bitmap));

            // imageM
            resizeBilinearFilter.NewWidth = imageL.Width / 2;
            resizeBilinearFilter.NewHeight = (int)(imageL.Width / 2 * aspectRatio);
            imageM = new BitmapUnsafe(resizeBilinearFilter.Apply(imageL.Bitmap));

            // imageS
            resizeBilinearFilter.NewWidth = imageM.Width / 2;
            resizeBilinearFilter.NewHeight = (int)(imageM.Width / 2 * aspectRatio);
            imageS = new BitmapUnsafe(resizeBilinearFilter.Apply(imageM.Bitmap));
        }

        public BitmapUnsafe GetImage(int brushSize)
        {
            // Return the next largest image
            if (brushSize > imageL.Width)
            {
                return imageXL;
            }
            else if (brushSize > imageM.Width)
            {
                return imageL;
            }
            else if (brushSize > imageS.Width)
            {
                return imageM;
            }
            else
            {
                return imageS;
            }
        }
    }
}
